package cn.com.mayue.guava.collections;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

import java.util.Collections;
import java.util.Map;

/**
 * Created by mayue on 2016/12/4.
 * 不可变集合
 * 可变集合接口	属于JDK还是Guava	不可变版本
 Collection	JDK	ImmutableCollection
 List	JDK	ImmutableList
 Set	JDK	ImmutableSet
 SortedSet/NavigableSet	JDK	ImmutableSortedSet
 Map	JDK	ImmutableMap
 SortedMap	JDK	ImmutableSortedMap
 Multiset	Guava	ImmutableMultiset
 SortedMultiset	Guava	ImmutableSortedMultiset
 Multimap	Guava	ImmutableMultimap
 ListMultimap	Guava	ImmutableListMultimap
 SetMultimap	Guava	ImmutableSetMultimap
 BiMap	Guava	ImmutableBiMap
 ClassToInstanceMap	Guava	ImmutableClassToInstanceMap
 Table	Guava	ImmutableTable

 无法通过实例对象进行操作，保证其不可变
 */
public class ImmutableMapTest {
    @Test
    public void testGuava() {
        ImmutableMap<String,String> immutableMap = ImmutableMap.of("1","s","2","t");
        //不可变的集合 发生变化 exp
//        immutableMap.put("1","error");
        System.out.println(JSON.toJSONString(immutableMap));


    }


    @Test
    public void testJdk(){
        Map<String,String> map = Maps.newHashMap();
        map.put("1","sss");
        //使用jdk Collections 转换
        Map<String,String> unmodifiableMap = Collections.unmodifiableMap(map);
        //也会抛exp
//        unmodifiableMap.put("1","555");
        System.out.println(JSON.toJSONString(map));
    }

}
